package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementQueryObject;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IUserService userService;

    private static final String prefix = "business/statementItem/";

    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String editDetail(Long statementId, Model model) {
        Statement statement = statementService.get(statementId);
        // 通过shiroutils获取当前登录客服的id，查询信息
        statement.setPayee(userService.get(ShiroUtils.getUserId()));
        // 还要查询信息显示到前台
        model.addAttribute("statement",statement);
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix +"editDetail";
        } else {
            return prefix +"showDetail";
        }
    }


    @RequiresPermissions("business:statementItem:query")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(Long statementId, StatementQueryObject qo) {
       return statementItemService.query(statementId,qo);
    }

    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> item) {
        statementService.saveItems(item);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {
        statementService.payStatement(statementId);
        return AjaxResult.success(statementId);
    }

}
